package com.furkid.service.controller;

import com.furkid.service.constant.AjaxResult;
import com.furkid.service.domain.pet.Pet;
import com.furkid.service.domain.pet.PetWeight;
import com.furkid.service.service.IPetService;
import com.furkid.service.web.context.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/pet")
public class PetController {
    @Autowired
    IPetService iPetService;

    /**
     * 获取宠护列表
     * @param pageNum 第几页
     * @param pageSize 一页的大小
     * @return 返回宠物列表
     */
    @GetMapping("/list")
    public AjaxResult listPets(@RequestParam(defaultValue = "1") int pageNum,
                               @RequestParam(defaultValue = "10") int pageSize) {
        return AjaxResult.success("获取宠物列表成功", iPetService.listPets(pageNum, pageSize));
    }

    @GetMapping("/search")
    public AjaxResult searchPetByName(@RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "10") int pageSize,
                                      @RequestParam String name) {
        return AjaxResult.success("查询成功!", iPetService.serachPetByName(pageNum, pageSize, name));
    }

    /**
     * 获取宠物类型列表
     * @return 返回宠物类型列表
     */
    @GetMapping("/list/type")
    public AjaxResult petTypeList() {
        return AjaxResult.success("获取宠物类型列表成功", iPetService.petTypeList());
    }

    /**
     * 获取宠物品种
     * @param petTypeId 宠物类型id
     * @return 返回宠物品种结果
     */
    @GetMapping("/list/breed")
    public AjaxResult petBreedList(@RequestParam Integer petTypeId) {
        return AjaxResult.success("获取宠物品种成功", iPetService.petBreedList(petTypeId));
    }

    /**
     * 添加宠物
     * @param pet 宠物信息
     * @return 返回添加结果
     */
    @PostMapping("/add")
    public AjaxResult addPet(@RequestBody Pet pet) {
        pet.setUser_id(UserContext.getUserId());
        iPetService.addPet(pet);
        return AjaxResult.success("添加宠物成功！");
    }

    /**
     * 删除错误
     * @param id 宠物id
     * @return 返回操作结果
     */
    @DeleteMapping("/del/{id}")
    public AjaxResult deletePet(@PathVariable Long id) {
        iPetService.deletePetById(id);
        return AjaxResult.success("删除宠物成功！");
    }

    /**
     * 查询宠物
     * @param id 宠物id
     * @return 返回宠物信息
     */
    @GetMapping("/info/{id}")
    public AjaxResult petInfoById(@PathVariable Long id) {
        return AjaxResult.success("查询成功", iPetService.searchPetById(id));
    }

    /**
     * 获取宠物体重列表
     * @param id 宠物id
     * @return 返回宠物体重列表结果
     */
    @GetMapping("/weight/list/{id}")
    public AjaxResult petWeightListById(@PathVariable Long id) {
        return AjaxResult.success("获取宠物体重列表成功!!!", iPetService.petWeightListById(id));
    }

    /**
     * 添加宠物体重
     * @param petWeight 宠物体重信息
     * @return 返回操作结果
     */
    @PostMapping("/weight/add")
    public AjaxResult addPetWeight(@RequestBody PetWeight petWeight) {
        iPetService.addOnePetWeight(petWeight);
        return AjaxResult.success("添加成功!");
    }

    /**
     * 更新宠物体重记录
     * @param petWeight 宠物体重信息
     * @return 返回操作结果
     */
    @PutMapping("/weight/update")
    public AjaxResult updatePetWeight(@RequestBody PetWeight petWeight) {
        iPetService.updateOnePetWeight(petWeight);
        return AjaxResult.success("修改成功!");
    }

    @DeleteMapping("/weight/delete/{id}")
    public AjaxResult deletePetWeight(@PathVariable Long id, @RequestParam Long petId) {
        iPetService.deleteOnePetWeight(id, petId);
        return AjaxResult.success("删除成功!!!");
    }
}
